package com.zl.gmadmin.vo.menu;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zl.gmadmin.service.dto.MenuDto;
import com.zl.gmadmin.utils.Builder;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangliang
 * @version 1.0
 * @date 2021/4/26 15:41
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class SysMenuVo {

    private Long id;

    private String title;

    private String name;

    private String component;

    private Long menuSort;

    private String icon;

    private Boolean iFrame;

    private Boolean cache;

    private Boolean hidden;

    private String permission;

    private LocalDateTime createTime;

    private String method;

    private String createBy;

    private String updateBy;

    private LocalDateTime updateTime;


    private Boolean hasChildren;

    private Boolean isLeaf;

    private String path;

    private Integer type;

    private Long pid;

    private List<SysMenuVo> children;

    public Boolean getLeaf() {
        return isLeaf;
    }

    public void setLeaf(Boolean leaf) {
        isLeaf = leaf;
    }

    public List<SysMenuVo> getChildren() {
        return children;
    }

    public void setChildren(List<SysMenuVo> children) {
        this.children = children;
    }

    public Boolean getHasChildren() {
        return hasChildren;
    }

    public void setHasChildren(Boolean hasChildren) {
        this.hasChildren = hasChildren;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public String getLabel() {
        return title;
    }

    public void setLabel(String label) {
        this.title = label;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public Long getMenuSort() {
        return menuSort;
    }

    public void setMenuSort(Long menuSort) {
        this.menuSort = menuSort;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Boolean getiFrame() {
        return iFrame;
    }

    public void setiFrame(Boolean iFrame) {
        this.iFrame = iFrame;
    }

    public Boolean getCache() {
        return cache;
    }

    public void setCache(Boolean cache) {
        this.cache = cache;
    }

    public Boolean getHidden() {
        return hidden;
    }

    public void setHidden(Boolean hidden) {
        this.hidden = hidden;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public Boolean getIsLeaf() {
        return isLeaf;
    }

    public void setIsLeaf(Boolean leaf) {
        isLeaf = leaf;
    }

    public  SysMenuVo buildMenu(MenuDto menuDto) {
        SysMenuVo sysMenuVo= Builder.of(SysMenuVo::new)
                .with(SysMenuVo::setId,menuDto.getId())
                .with(SysMenuVo::setName,menuDto.getComponentName())
                .with(SysMenuVo::setLabel,menuDto.getTitle())
                .with(SysMenuVo::setIcon,menuDto.getIcon())
                .with(SysMenuVo::setComponent,menuDto.getComponent())
                .with(SysMenuVo::setPermission,menuDto.getPermission())
                .with(SysMenuVo::setMethod,menuDto.getMethod())
                .with(SysMenuVo::setMenuSort,menuDto.getMenuSort())
                .with(SysMenuVo::setiFrame,menuDto.getiFrame())
                .with(SysMenuVo::setCache,menuDto.getCache())
                .with(SysMenuVo::setHidden,menuDto.getHidden())
                .with(SysMenuVo::setCreateTime,menuDto.getCreateTime())
                .with(SysMenuVo::setCreateBy,menuDto.getCreateBy())
                .with(SysMenuVo::setUpdateTime,menuDto.getUpdateTime())
                .with(SysMenuVo::setUpdateBy,menuDto.getUpdatedBy())
                .with(SysMenuVo::setPath,menuDto.getPath())
                .with(SysMenuVo::setType,menuDto.getType())
                .with(SysMenuVo::setPid,menuDto.getPid())
                .with(SysMenuVo::setHasChildren, menuDto.getSubCount()>0? true:false)
                .with(SysMenuVo::setIsLeaf,menuDto.getSubCount()>0? false:true)
                .build();

           if (Objects.nonNull(menuDto.getChildren())){
               List<SysMenuVo> sysMenuVos=new ArrayList<>();
               for (MenuDto m: menuDto.getChildren()
                    ) {
                   sysMenuVos.add(buildMenu(m));
               }
               sysMenuVo.setChildren(sysMenuVos);
           }

        return sysMenuVo;
    }
}
